package com.guigu.buy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.buy.mapper.OrderDetailsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.buy.dto.CellUtils;
import com.guigu.buy.dto.OrderUtil;
import com.guigu.buy.dto.Order_Merchant;
import com.guigu.buy.pojo.Order;
import com.guigu.buy.mapper.OrderMapper;
import com.guigu.buy.pojo.OrderDetails;
import com.guigu.buy.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.buy.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.guigu.buy.vo.OrderDetailsVo;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailsMapper orderDetailsMapper;
    @Override
    public List<Order> selectByStatus(Order order) {
        return orderMapper.selectByStatus(order);
    }

    @Override
    public OrderDetailsVo getOrderDetailsById(Integer orderId) {
        Order order = orderMapper.getOrderById(orderId);
        List<OrderDetails> details = orderDetailsMapper.findByOrderId(orderId);
        OrderDetailsVo vo = new OrderDetailsVo();
        vo.setOrdersId(order.getId());
        vo.setOrder_id(order.getOrderId());
        vo.setDetails(details);
        return vo;
    }

    @Override
    public String getMaxStockId() {
        //查询ID最大的仓库信息
        Order order = orderMapper.getMaxStockId();
        if (order == null) {
            return null;
        }
        //获得最大的仓库编号
        String orderId = order.getOrderId();
        return orderId;
    }

    @Override
    public PageInfo<OrderUtil> queryAll(int pageNo, int pageSize, OrderVo vo) {
        List<OrderUtil> list=orderMapper.query(vo);
        PageHelper.startPage( pageSize,pageNo);
        PageInfo<OrderUtil> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Order_Merchant> check(OrderVo order, int cid, int pageNo, int pageSize) {
        //根据商户编号查询订单表
        int merchantId=orderMapper.queryMerchantId(cid);
order.setMerchantId(merchantId);
        List<Order_Merchant> list=orderMapper.list(order);
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Order_Merchant> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    //查询待收货订单P001-3

    @Override

    public PageInfo<Order> shohuodindan(int pageNo, int pageSize,int cid) {
        int merchantId = orderMapper.queryMerchantId(cid);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("convey_tag","P001-3");
        wrapper.eq("merchant_id",merchantId);
        List<Order> list = this.list(wrapper);
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<Order> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    //查询已收货订单P001-4

    @Override
    public PageInfo<Order> mechant(int pageNo, int pageSize,int cid) {
        int merchantId = orderMapper.queryMerchantId(cid);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("merchant_id",merchantId);
        wrapper.eq("convey_tag","P001-4");
        List<Order> list=this.list(wrapper);
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Order> pageInfo=new PageInfo<>(list);
        return  pageInfo;
    }

    //查询已提货订单表P001-2

    @Override
    public PageInfo<Order> pickupcx(int pageSize, int pageNo,int cid) {
        int merchantId = orderMapper.queryMerchantId(cid);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("merchant_id",merchantId);
        wrapper.eq("convey_tag","P001-2");
        List<Order> list=this.list(wrapper);
        PageHelper.startPage(pageSize,pageNo);
        PageInfo<Order> pageInfo=new PageInfo<>(list);
        return  pageInfo;
    }
}
